#include "TBranch.h"
TFile *f;
TTree *t;
void makeHist() 
{
	printf("Type a number of the dependency which you want to get\n");
	printf("1-LC(R) lSc,lAbs=const;\n2-LC(lSc) R,lAbs=const;\n3 - LC(LAbs) R,lSc=const\n");
	Int_t choise=0;
	scanf("%d",&choise);
	f = new TFile("~/tree1.root");
	t = (TTree*)f->Get("aTree");
	TBranch *lSc_b = t->GetBranch("lSc");
	TBranch *lC_b = t->GetBranch("lightCollection");
	TBranch *R_b = t->GetBranch("R");
	TBranch *lAbs_b = t->GetBranch("lAbs");

	Double_t lScfix,lAbsfix,Rfix,lSc,lAbs,R;
	switch(choise)
	{
	case 1:


		Double_t min,max;
		lSc_b->SetAddress(&min);
		lSc_b->GetEntry(0);
		lSc_b->SetAddress(&max);
		lSc_b->GetEntry(lSc_b->GetEntries()-1);


		printf("Enter number between %f and %f in order to fix scaterring length\n",min,max);
		Float_t num;
		scanf("%f",&num);
		lScfix = findClosestVal(lSc_b,num);
		printf("lSc assumed equal %f\n",lScfix);

		lAbs_b->SetAddress(&min);
		lAbs_b->GetEntry(0);
		lAbs_b->SetAddress(&max);
		lAbs_b->GetEntry(t->GetEntries()-1);
		printf("Enter number between %f and %f in order to fix absorption length\n",min,max);
		scanf("%f",&num);
		lAbsfix = findClosestVal(lAbs_b,num);
		printf("lAbs assumed equal %f\nNow building a graph\n",lAbsfix);
		R_b->SetAddress(&min);
		R_b->GetEntry(0);
		R_b->SetAddress(&max);
		R_b->GetEntry(t->GetEntries()-1);
		DrawHist(R_b,lSc_b,lAbs_b,lScfix,lAbsfix,"LC(R)","R","LC");
		break;
	case 2:
		Double_t min,max;
		R_b->SetAddress(&min);
		R_b->GetEntry(0);
		R_b->SetAddress(&max);
		R_b->GetEntry(R_b->GetEntries()-1);


		printf("Enter number between %f and %f in order to fix the radius\n",min,max);
		Float_t num;
		scanf("%f",&num);
		Rfix = findClosestVal(R_b,num);
		printf("R assumed equal %f\n",Rfix);

		lAbs_b->SetAddress(&min);
		lAbs_b->GetEntry(0);
		lAbs_b->SetAddress(&max);
		lAbs_b->GetEntry(t->GetEntries()-1);
		printf("Enter number between %f and %f in order to fix absorption length\n",min,max);
		scanf("%f",&num);
		lAbsfix = findClosestVal(lAbs_b,num);
		printf("lAbs assumed equal %f\nNow building a graph\n",lAbsfix);
		DrawHist(lSc_b,R_b,lAbs_b,Rfix,lAbsfix,"LC(L_Sc)","L_Sc","LC");
		break;
	case 3:
		Double_t min,max;
		R_b->SetAddress(&min);
		R_b->GetEntry(0);
		R_b->SetAddress(&max);
		R_b->GetEntry(R_b->GetEntries()-1);


		printf("Enter number between %f and %f in order to fix the radius\n",min,max);
		Float_t num;
		scanf("%f",&num);
		Rfix = findClosestVal(R_b,num);
		printf("R assumed equal %f\n",Rfix);

		lSc_b->SetAddress(&min);
		lSc_b->GetEntry(0);
		lSc_b->SetAddress(&max);
		lSc_b->GetEntry(t->GetEntries()-1);
		printf("Enter number between %f and %f in order to fix scaterring length\n",min,max);
		scanf("%f",&num);
		lScfix = findClosestVal(lSc_b,num);
		printf("lSc assumed equal %f\nNow building a graph\n",lScfix);
		DrawHist(lAbs_b,R_b,lSc_b,Rfix,lScfix,"LC(L_Abs)","L_Abs","LC");
		break;
	default:
		break;


	}

}
void DrawHist(TBranch* var_b,TBranch* fix1_b,TBranch* fix2_b,Double_t fix1,Double_t fix2,const char*title ="graph",const char* xtitle="x",const char* ytitle="y")
{
	TBranch *lC_b = t->GetBranch("lightCollection");
	Double_t v1,v2,y,x;
	fix1_b->SetAddress(&v1);
	fix2_b->SetAddress(&v2);
	lC_b->SetAddress(&y);
	var_b->SetAddress(&x);
	const Int_t numOfEvt = 1000;
	Int_t eventIndex[numOfEvt];
	Int_t numOfMatches=0;
	for(Int_t i=0;i<t->GetEntries();i++)
	{
		fix1_b->GetEntry(i);
		fix2_b->GetEntry(i);
		if(v1==fix1&&v2==fix2)
		{
			eventIndex[numOfMatches]=i;
			numOfMatches++;
		}
	}

	Double_t xarr[numOfEvt], yarr[numOfEvt];
	for(Int_t i=0;i<=numOfMatches;i++)
	{
		var_b->GetEntry(eventIndex[i]);
		lC_b->GetEntry(eventIndex[i]);
		xarr[i] = x;
		yarr[i] = y;
		printf("%f-x,%f-y\n",x,y);
	}

	TCanvas *c1 = new TCanvas("c1","c1",600,800);

	TGraph* gr = new TGraph(numOfMatches,xarr,yarr);
	gr->SetLineColor(2);
   	gr->SetLineWidth(2);
   	gr->SetMarkerColor(4);
   	gr->SetMarkerStyle(21);
   	gr->SetTitle(title);
   	gr->GetXaxis()->SetTitle(xtitle);
   	gr->GetYaxis()->SetTitle(ytitle);
   	gr->Draw("ACP");

}

Double_t findClosestVal(TBranch* b,Double_t val)
{
	Int_t n = b->GetEntries();
	Double_t currVal;
	b->SetAddress(&currVal);
	b->GetEntry(0);
	Double_t delta1 = TMath::Abs(val-currVal);

	Int_t min;
	for(int i=1;i<n;i++)
	{
		b->GetEntry(i);
		if(TMath::Abs(currVal-val)<delta1)
		{
			delta1 = TMath::Abs(currVal-val);
			min = i;
		}

	}
	b->GetEntry(min);
	return currVal;

}

